package cn.maihe.elg.operation.centers.hymake.handler.service;

import cn.maihe.elg.operation.centers.hymake.config.HyMakeInfoConfig;
import cn.maihe.elg.operation.centers.hymake.dto.HyMakeBaseRespDTO;
import cn.maihe.elg.operation.centers.hymake.dto.handler.HyMakeClaimApplyReqDTO;
import cn.maihe.elg.operation.centers.hymake.dto.handler.HyMakeDelApplyReqDTO;
import cn.maihe.elg.operation.centers.hymake.dto.handler.HyMakeRevokeApplyReqDTO;
import cn.maihe.elg.operation.centers.hymake.handler.HyMakeCenterTradeService;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.enums.*;
import cn.maihe.elg.operation.repository.entity.*;
import cn.maihe.elg.operation.service.business.InstitutionService;
import cn.maihe.elg.operation.service.business.ProjectInfoService;
import cn.maihe.elg.operation.service.guarantee.GuaranteeService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.service.order.ApplyOrderInfoService;
import cn.maihe.elg.operation.service.order.RefundOrderInfoService;
import cn.maihe.elg.operation.utils.IdGenerate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * @Description 保函理赔申请
 * @Author caixh
 * @Date 2022/3/30.
 */
@CenterTradeService(
        provider = CenterProviderEnum.HYMAKE,
        centerNo = CenterNoEnum.NINGDE,
        tradeType = TradeType.lstDel,
        reqClass = HyMakeDelApplyReqDTO.class,
        respClass = HyMakeBaseRespDTO.class
)
@Slf4j
public class HyMakeDelApplyService extends HyMakeCenterTradeService<HyMakeDelApplyReqDTO, HyMakeBaseRespDTO> {

    @Resource
    ApplyOrderInfoService applyOrderInfoService;
    @Resource
    AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    RefundOrderInfoService refundOrderInfoService;
    @Resource
    ProjectInfoService projectInfoService;
    @Resource
    private InstitutionService institutionService;
    @Resource
    GuaranteeService guaranteeService;

    public HyMakeDelApplyService(HyMakeInfoConfig centerInfosConfig) {
        super(centerInfosConfig);
    }


    /**
     * 具体业务处理
     *
     * @param context
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public HyMakeBaseRespDTO processService(CenterTradeContext<HyMakeDelApplyReqDTO, HyMakeBaseRespDTO> context) {

        HyMakeDelApplyReqDTO req = context.getReq();
        Long centerId = context.getCenterId();
        Long orgId = context.getOrgId();
        String acceptOrderNo = req.getInsuranceNo();

//        AcceptOrderInfoDO orderInfoDO = acceptOrderInfoService.getByGuaranteeNo(centerId, orgId, revokeNo);
        AcceptOrderInfoDO orderInfoDO = acceptOrderInfoService.getByAcceptOrderNo(acceptOrderNo);
        if (orderInfoDO == null) {
            return HyMakeBaseRespDTO.error("查无此投保申请记录");
        }
        AcceptOrderStatus acceptOrderStatus = AcceptOrderStatus.resolve(orderInfoDO.getStatus());
        if (orderInfoDO.getStatus() > AcceptOrderStatus.WAIT_PAY_CONFIRM.getStatus()) {
            return HyMakeBaseRespDTO.error("订单状态为[" + acceptOrderStatus + "]，不允许删除");
        }

        AcceptOrderInfoDO updateOrderInfo = AcceptOrderInfoDO.builder().id(orderInfoDO.getId())
                .status(AcceptOrderStatus.CANCEL.getStatus()).remark("出函前，电子保函平台允许投标企业删除保函订单").build();
        acceptOrderInfoService.updateById(updateOrderInfo);
        return HyMakeBaseRespDTO.success("允许删除订单，交易已取消");
    }
}
